package com.anycc.cloud.core.controller;

import com.anycc.cloud.common.dto.datatable.DataTableRequest;
import com.anycc.cloud.common.dto.datatable.DataTableResponse;
import com.anycc.cloud.common.dto.easyui.Tree;
import com.anycc.cloud.common.dto.json.ListResponse;
import com.anycc.cloud.common.dto.json.ObjectResponse;
import com.anycc.cloud.common.dto.json.Response;
import com.anycc.cloud.common.dto.json.SuccessResponse;
import com.anycc.cloud.common.dto.query.OrderBy;
import com.anycc.cloud.core.entity.Org;
import com.anycc.cloud.core.entity.OrgType;
import com.anycc.cloud.core.service.OrgService;
import com.anycc.cloud.core.service.OrgTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * org controller
 */
@RestController
@RequestMapping("org")
public class OrgController {
    @Autowired
    private OrgService orgService;
    @Autowired
    private OrgTypeService orgTypeService;

    /**
     * create a new org
     *
     * @param org org
     * @return Response
     */
    @PostMapping("create")
    public Response create(Org org) {
        orgService.create(org);
        return new SuccessResponse();
    }

    /**
     * build org tree
     *
     * @return tree
     */
    @GetMapping("tree")
    public Response tree() {
        return new ListResponse<>(orgService.tree());
    }

    /**
     * build parent org tree
     *
     * @return tree
     */
    @GetMapping("tree/parent")
    public List<Tree> parentTree(@RequestParam("id") String id) {
        return orgService.parentTree(id);
    }

    /**
     * datatable paging query
     *
     * @param org     Org
     * @param request DataTableRequest
     * @return DataTableResponse
     */
    @GetMapping("findByPaging")
    public DataTableResponse<Org> findByPaging(Org org, DataTableRequest request) {
        List<OrderBy> orders = new ArrayList<>();
        orders.add(new OrderBy("desc", "createTime"));
        return new DataTableResponse<Org>(this.orgService.findByPaging(org, request.buildPageRequest(orders)), request);
    }

    /**
     * delete org by id
     *
     * @param id org id
     * @return response
     */
    @PostMapping("delete")
    public Response delete(@RequestParam("id") String id) {
        orgService.delete(id);
        return new SuccessResponse();
    }

    /**
     * find org by id
     *
     * @param id id
     * @return Org
     */
    @GetMapping("findById")
    public Org findById(@RequestParam("id") String id) {
        return orgService.findById(id);
    }

    /**
     * update org
     *
     * @param org org
     * @return response
     */
    @PostMapping("update")
    public Response update(Org org) {
        orgService.update(org);
        return new SuccessResponse();
    }

    /**
     * find orgType list
     *
     * @return Response
     */
    @GetMapping("type/list")
    public Response findOrgTypeList() {
        return new ListResponse<>(orgTypeService.findAllOrderByNameAsc());
    }

    /**
     * create orgType
     *
     * @param orgType orgType
     * @return Response
     */
    @PostMapping("type/create")
    public Response createOrgType(OrgType orgType) {
        return new ObjectResponse<>(this.orgTypeService.create(orgType));
    }

    /**
     * update orgType
     *
     * @param orgType orgType
     * @return Response
     */
    @PostMapping("type/update")
    public Response updateOrgType(OrgType orgType) {
        return new ObjectResponse<>(this.orgTypeService.update(orgType));
    }

    /**
     * delete orgType
     *
     * @param orgTypeId orgType id
     * @return Response
     */
    @PostMapping("type/delete")
    public Response deleteOrgType(@RequestParam("orgTypeId") String orgTypeId) {
        orgTypeService.delete(orgTypeId);
        return new SuccessResponse();
    }

}
